let express = require("express");
let router = express.Router();
let bcrypt = require("bcryptjs");
let models = require("../models");
let jwt = require("jsonwebtoken");
const Sequelize = require('sequelize');
const { leave } = require('../database/init')
const Op = Sequelize.Op;
let { SECRET_KEY, EXPIRES_TIME } = require("../setting/constant.js");
// let Result = require("../utils/Result.js");

const secret = "wen_huan_shi_shuai_ge_ma";

// 测试
router.post("/", async function (req, res, next) {
  res.send('ok');

});

/*获取该老师所属班级的全部请假单与数量(分页查询)*/
router.post("/getLeave", async (req, res) => {
  let { currPage, pageNum } = req.body
  currPage = pageNum * currPage
  let leaves = await leave.findAll({ offset: currPage, limit: pageNum })
  /*const amount = await leave.findAll({})
  leaves.total = amount.length
 console.log(leaves.total)*/
  res.send(leaves)
});
/*审批假条总页数 */
router.post("/getLeaveTotal", async (req, res) => {
  let amount = await leave.findAll({})
  res.send(amount)
});
/*老师对假条进行审批 */
router.post("/upLeaveState", async (req, res) => {
  let { id, state } = req.body
  await leave.update({ state: state }, {
    where: {
      id: id
    }
  });
  res.send('ok')
});
router.post("/getUserLeave", async (req, res) => {
  let { currPage, pageNum, id } = req.body
  currPage = pageNum * currPage
  let leaves = await leave.findAll({
    where: {
      stu_id: id
    },offset: currPage, limit: pageNum,
  })
  /*const amount = await leave.findAll({})
  leaves.total = amount.length
 console.log(leaves.total)*/
  res.send(leaves)
  /*res.send('ok')*/
});
/*获取该学生数据总条数 */
router.post("/getUserLeaveTotal", async (req, res) => {
  let {id} = req.body
  let amount = await leave.findAll({
    where: {
      stu_id: id
    }
  })
  res.send(amount)
});
/*reason: this.textarea,
 eavetype: this.value,
  starttime: starttime,
  endtime: endtime */
  router.post("/setApplication", async (req, res) => {
    let { reason, leavetype ,starttime,endtime,id,createtime} = req.body
    let leaves = await leave.create({ stu_id:id,reason:reason, leavetype:leavetype ,starttime:starttime,endtime:endtime,state:0,createtime:createtime});
    res.send('ok')
  });

module.exports = router;
